package com.mibao.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import com.learnncode.mediachooser.activity.HomeAddPicFragmentActivity;
import com.mibao.sketch.HideAppItem;
import com.mipai.R;

/**
 * Created by tianweishu on 2014/10/27.
 */
public class HideAppActivity extends Activity {

    private List<HideAppItem> hideAppItems;
    private HideAppAdapter adapter;
    private GridView gridView;
    private LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hideapp_layout);
        initTitle();
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        // 添加按钮
        HideAppItem item = new HideAppItem();
        item.setIcon(getResources().getDrawable(R.drawable.hideapp_add));
        
        hideAppItems = new ArrayList<HideAppItem>();
        //这里做些添加入库的操作
        hideAppItems.add(item);
        adapter = new HideAppAdapter();
        gridView = (GridView) findViewById(R.id.hidapp_grid);
        gridView.setAdapter(adapter);

        refresh();
    }
    
    private void initTitle(){
        ((ImageView)findViewById(R.id.head_right_img)).setImageResource(R.drawable.gallery_add);
        findViewById(R.id.head_right).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HideAppActivity.this, allAppListActivity.class);
                startActivity(intent);
            }
        });
    }
    private void refresh(){
        List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
        for(int i=0;i<packages.size();i++) {
            PackageInfo packageInfo = packages.get(i);
            HideAppItem item = new HideAppItem();
            item.appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
            item.pkgName = packageInfo.packageName;
            item.icon = packageInfo.applicationInfo.loadIcon(getPackageManager());
            if((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)==0)
            {
                hideAppItems.add(hideAppItems.size() - 1, item);//如果非系统应用，则添加至appList
            }

        }

        adapter.notifyDataSetChanged();
    }
    public class HideAppAdapter extends BaseAdapter{

        public HideAppAdapter() {
        }

        @Override
        public int getCount() {
            return hideAppItems.size();
        }

        @Override
        public Object getItem(int i) {
            return hideAppItems.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            if (convertView == null || convertView.getTag() == null){
                convertView = inflater.inflate(R.layout.hideapp_list_item, null);
            }
            // 设置图标
            convertView.findViewById(R.id.hideapp_item_img).setBackground(hideAppItems.get(i).getIcon());
            TextView appTitle = (TextView)convertView.findViewById(R.id.hideapp_item_title);
            appTitle.setText(hideAppItems.get(i).getAppName());
            return convertView;
        }
    }

}
